﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.Text;
using Newtonsoft.Json;
using System.IO;
using DbCommon;

namespace Web
{
    /// <summary>
    /// page 的摘要说明
    /// </summary>
    public class page : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //不让浏览器缓存
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";
            int pageindex;
            string SelectList = "*"; //列名
            int.TryParse(context.Request["p"], out pageindex);//获取请求页码
            string order = "CON_ID ASC";//初始化排序字段
            string tableName = context.Request["tableName"];//获得表名
            int pageSize = 5;//默认的是5
            string SearchCondition = ""; //查询条件
            if(!string.IsNullOrEmpty(context.Request["SelectList"]))
                SelectList = context.Request["SelectList"];
            if (!string.IsNullOrEmpty(context.Request["pageSize"]))
            {
                int.TryParse(context.Request["pageSize"], out pageSize);
            }
            if (!string.IsNullOrEmpty(context.Request["SearchCondition"]))
                SearchCondition = context.Request["SearchCondition"];
            if (!string.IsNullOrEmpty(context.Request["orderby"]))
            {
                string[] strarr = context.Request["orderby"].ToString().Split('%');
                if (strarr[1] == "0")
                    order = strarr[0] + " asc";
                else
                    order = strarr[0] + " desc";
            }
            if (pageindex == 0)
                pageindex = 1;
            DataTable dt = GetDataSet(pageindex, SelectList, order, tableName, pageSize, SearchCondition).Tables[0];
            string jsonData =JsonHelper.DataTableToJSON(dt, "tempTable");
            context.Response.Write(jsonData);
        }
        /// <summary>
        /// 根据帅选获取数据缓存集
        /// </summary>
        /// <param name="pageIndex">页码 </param>
        /// <param name="order">排序字段</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet(int pageIndex, string SelectList, string order, string tableName, int pageSize, string SearchCondition)
        {
            //输入参数
            SQLHelper.Parameter[] parameter = new SQLHelper.Parameter[]
            {
                new SQLHelper.Parameter("@SelectList",DbType.String,2000,ParameterDirection.Input,SelectList),
                new SQLHelper.Parameter("@TableSource",DbType.String,200,ParameterDirection.Input,tableName),
                new SQLHelper.Parameter("@SearchCondition",DbType.String,2000,ParameterDirection.Input,SearchCondition),
                new SQLHelper.Parameter("@OrderExpression",DbType.String,2000,ParameterDirection.Input,order),
                new SQLHelper.Parameter("@PageIndex",DbType.Int32,4,ParameterDirection.Input,pageIndex),
                new SQLHelper.Parameter("@PageSize",DbType.Int32,4,ParameterDirection.Input,5)
            };
            SQLHelper.OutputParameter outparam = new SQLHelper.OutputParameter();
            return SQLHelper.ExecuteStoredProcedureWithQuery("GetRecordFromPage2005", ref outparam, parameter);
            
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}